//
//  Fraction.m
//  02-ClassAndObject
//
//  Created by 李昊林 on 16/7/5.
//  Copyright © 2016年 李昊林. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction

- (instancetype)initWithNumerator:(NSInteger)numerator
                      denominator:(NSInteger)denominator
{
    self = [super init];
    if (self) {
        _numerator = numerator;
        _denominator = denominator;
    }
    return self;
}

+ (instancetype)fractionWithNumerator:(NSInteger)numerator
                          denominator:(NSInteger)denominator {
    Fraction *frc = [[Fraction alloc]initWithNumerator:numerator denominator:denominator];
    return frc;
}

- (NSInteger)gysWithNumber1:(NSInteger)number1
                    Number2:(NSInteger)number2{
    while (number1 % number2) {
        NSInteger temp = number1;
        number1 = number2;
        number2 = temp % number2;
    }
    return number2;
}

- (void)reduction {
    NSInteger gys = [self gysWithNumber1:self.numerator Number2:self.denominator];
    self.numerator /= gys;
    self.denominator /= gys;
    
}

- (Fraction *)addWith:(Fraction *)anotherFraction{
    NSInteger newNumerator = self.numerator * anotherFraction.denominator + self.denominator * anotherFraction.numerator;
    NSInteger newDenominator = self.denominator * anotherFraction.denominator;
    
    Fraction *result = [Fraction fractionWithNumerator:newNumerator denominator:newDenominator];
    [result reduction];
    return result;
}

-(Fraction *)subWith:(Fraction *)anotherFraction{
    NSInteger newNumerator = self.numerator *anotherFraction.denominator - self.denominator *anotherFraction.numerator;
    NSInteger newDenominator = self.denominator * anotherFraction.denominator;
    Fraction *result = [Fraction fractionWithNumerator:newNumerator denominator:newDenominator];
    [result reduction];
    return result;
}

- (Fraction *)mulWith:(Fraction *)anotherFraction{
    NSInteger newNumerator = self.numerator *anotherFraction.numerator;
    NSInteger newdenominator = self.denominator * anotherFraction.denominator;
    Fraction * result = [[Fraction alloc]initWithNumerator:newNumerator denominator:newdenominator];
    [result reduction];
    return result;
}

- (Fraction *)divWith:(Fraction *)anotherFraction {
    NSInteger newnumerator = self.numerator *anotherFraction.denominator;
    NSInteger newdenominator = self.denominator *anotherFraction.numerator;
    Fraction * result = [[Fraction alloc]initWithNumerator:newnumerator denominator:newdenominator];
    [result reduction];
    return result;
}

- (void)output{
    NSLog(@"result:%ld/%ld",self.numerator,self.denominator);
}






@end
